package com.graduation.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.model.R;
import com.graduation.ucenter.mapper.RoleMapper;
import com.graduation.ucenter.model.po.Authority;
import com.graduation.ucenter.model.po.Role;
import com.graduation.ucenter.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 小何
 * @Description: TODO
 * @DateTime: 2023/10/31 14:54
 **/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public R<List<Role>> listRole(Integer page, Integer pageSize, Role role) {
        LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper<>();
        if (role != null) {
            qw.like(role.getDescription() != null && !"".equals(role.getDescription()), Role::getDescription, role.getDescription())
                    .like(role.getName() != null && !"".equals(role.getName()), Role::getName, role.getName())
                    .eq(role.getId() != null, Role::getId, role.getId());
        }
        Page<Role> rolePage = new Page<>();
        this.page(rolePage, qw);
        Map<String, Object> map = new HashMap<>();
        map.put("count", rolePage.getTotal());
        return R.success(rolePage.getRecords(), map);
    }

    @Override
    public R<List<Role>> listRoleByUserId(Integer userId) {
        List<Role> roles = roleMapper.listRoleByUserId(userId);
        return R.success(roles);
    }

    @Override
    public R<List<Authority>> listAuthorityByRoleId(Integer roleId) {
        List<Authority> authorities = roleMapper.listAuthorityByRoleId(roleId);
        return R.success(authorities);
    }

    @Override
    public R<String> addBindAuthorityByRoleId(Integer authorityId, Integer roleId) {
        int i = roleMapper.judgeAuthorityRoleContains(roleId, authorityId);
        if (i != 0) {
            return R.error("已绑定该权限");
        }
        roleMapper.addBindAuthorityByRoleId(authorityId, roleId);
        return R.success("绑定成功");
    }

    @Override
    public R<String> removeBindAuthorityByRoleId(Integer roleId, Integer authorityId) {
        roleMapper.removeBindAuthorityByRoleId(authorityId, roleId);
        return R.success("取消绑定成功");
    }

    @Override
    public R<String> addRole(Role role) {
        LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper<>();
        qw.eq(role != null && role.getName() != null && !"".equals(role.getName()), Role::getName, role.getName());
        Role one = this.getOne(qw);
        if(one!=null){
            return R.error("角色已存在");
        }
        this.save(role);
        return R.success("角色添加成功");
    }

}
